import JDBCunit.Jdbcs;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBCTest06 {
    public static void main(String[] args) {
        Connection cn = null;
        PreparedStatement ps = null;
        try {
            cn = Jdbcs.jC();
            cn.setAutoCommit(false);//开启事务
            ps = cn.prepareStatement("update t_aco set bal = ? where aco = ?");
            //张三给李四转10000元
            ps.setInt(1,10000);
            ps.setString(2,"zhangsan");



            ps.setInt(1,10000);
            ps.setString(2,"lisi");
            ps.executeUpdate();
            cn.commit();//全部执行成功,成功提交

        } catch (SQLException e) {
            try {
                cn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            //否则回滚,回到开启事务前，保证数据安全

            e.printStackTrace();
        } catch (NullPointerException e) {
            try {
                System.out.println("zhuazhule");
                cn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }

            e.printStackTrace();
        }
        finally {
            try {
                Jdbcs.jClose(null,ps,cn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}


class Test {
    public static void main(String[] args) {

    }



}
